import * as types from './types'

export const state = {
  member: {
    members: [],
    loading: false,
    query: {
      condition: ''
    },
    curPage: 1,
    total: 0,
    pageSize: 10
  }
}

export const getters = {
  member: state => state.member
}

export const actions = {
  getMemberList({
    commit
  }, payload) {
    commit(types.GET_MEMBER_LIST, payload);
  }
}

export const mutations = {
  [types.GET_MEMBER_LIST](state, {
    vm,
    page
  }) {
    state.member.loading = true;
    const params = {
      limit: state.member.pageSize,
      condition: state.member.query.condition
    };
    params.start = page ? page : state.member.curPage;
    vm.$ajax.get('/member/query', {
        params
      })
      .then(res => {
        let data = res.data;
        if (data && data.status == vm.$appConfig.success) {
          state.member.members = data.list;
          state.member.total = data.total;
          state.member.pageSize = data.pageSize;
          state.member.curPage = data.curPage;
          state.member.loading = false;
        } else {
          state.member.members = [];
          state.member.loading = false;
        }
      })
      .catch(err => {
        state.member.members = [];
        state.member.loading = false;

        vm.$notify.error({
          title: '失败',
          message: '获取会员列表失败'
        });
        console.error(err);
      });
  }
}